<?php
include_once('./incl/winbinder/winbinder.php');

define('ACC_NEXT_CTRL'	,	10001);
define('ACC_PREV_CTRL'	,	10002);
define('ACC_SELECT_ALL'	,	10003);

function createGuiWindow() {
	
	global $winmain, $controls;
	
	include_once('./incl/tk_wb_guidef.php');
	
	createMainWindow();
	setGuiInitialState();
	
	wb_set_handler($winmain,'guiHandler');
	wb_main_loop();
}

function guiHandler($window, $id, $ctrl, $param1 = 0, $param2 = 0) {

	global $controls, $winmain;

	// Procesam in functie de id-ul actiunii sau 
	// controlului pe care s-a efectuat actiunea
    switch($id) {
    	//-----Keyboard Interaction (Obiecte Accel)-----
    	case ACC_NEXT_CTRL:		nextControl(ACC_NEXT_CTRL); 	break;
    	case ACC_PREV_CTRL: 	nextControl(ACC_PREV_CTRL); 	break;	
    	case ACC_SELECT_ALL: 	nextControl(ACC_SELECT_ALL); 	break;

    	//-----Object Interaction (celelalte obiecte din Form)-----
    	case TK_COMBO_01NEW: parentComboChanged(); break;

    	case TK_BUTTON_01:
    		// Accesarea butonului de submit duce la o
    		// simpla procesare a informatiilor urmata de
    		// trimiterea informatiilor catre toolKit
    		$IsValid 	= performDataValidation();
    		if( $IsValid )
    		{
    			sendFormData();
    		}
    		break;

    	// Constanta IDCLOSE este predefinita 
    	// pentru butonul de inchidere a ferestrei
		case IDCLOSE: wb_destroy_window($window); break;

		default:
			// Acceleratorii trebuiesc redefiniti 
			// de fiecare data in functia de procesare
			wb_create_control($winmain, Accel, array(
				array( ACC_NEXT_CTRL, "Tab" ),
				array( ACC_PREV_CTRL, "Shift+Tab" ),
				array( ACC_SELECT_ALL,"Ctrl+A")
				));
			break;
	}
}

function nextControl($accel) {
	// Extensia ce ne permite implementarea Win32API folosind PHP
	// nu are inca corect implementate conceptele de Tab Order si de
	// selectie a informatiilor folosind CTRL+A. Aceasta metoda
	// le implementeaza folosind implementarea WinBinder pentru 
	// keyboard shortcuts (obiecte Accel).
	// Implementarea de fata nu este una generala ci mai degraba
	// un quick-fix special pentru necesitatile GUI ale ToolKitului
	global $winmain, $controls;
	
	static $number = 1;		//Contor static ce tine evidenta controlului la care avem focus
	
	$focus = wb_get_focus();// Luam ID-ul controlului actual selectat
	$i = 0;					// Contor ce ne permite sa luam numarul de ordine al elementului
	foreach($controls['edit'] as $ctrl) {
		$i++;
		if ($ctrl == $focus) {
			$number = $i;	// Modificam number cu numarul de ord al controlului cu focus
		}
	}
	
	($accel == ACC_NEXT_CTRL) 	? $number++ : null;	// Next control (TAB)
	($accel == ACC_PREV_CTRL) 	? $number-- : null;	// Previous control (SHIFT + TAB)
	($accel == ACC_SELECT_ALL) 	? null 		: null; // Select All (CTRL + A)
	
	if($number > 6) $number = 1;
	if($number < 1) $number = 6;

	switch($number) {
		case 1: $id = TK_EDIT_01NAME; break;
		case 2: $id = TK_EDIT_02PREFIX; break;
		case 3: $id = TK_EDIT_03LANG; break;
		case 4: $id = TK_EDIT_04COPY; break;
		case 5: $id = TK_EDIT_05DESC; break;
		case 6: $id = TK_EDIT_06KEYW; break;
		default: echo "Error With AutoFocus!"; break; 	
	}

	wb_set_focus($controls['edit'][$id]); 
}

// Definirea stringurilor pentru erori;
define("ERR_NO_APP_NAME"		, "->You need to enter a name for your application!\n");
define("ERR_WRONG_APP_NAME"		, "->You can only have alphanumeric symbols in your name,\n and first character should be alphanumeric\n");
define("ERR_NO_APP_PREFIX"		, "-> Prefix is required for an application\n");
define("ERR_LONG_APP_PREFIX"	, "-> Your prefix is too long, maximum 5 chars accepted \n");
define("ERR_WRONG_APP_PREFIX"	, "-> You can only have alphanumeric symbols in your prefix,\n and first character should be a letter\n");
define("ERR_NO_ITEM_NAME"		, "-> You need to enter a name for your item!\n");
define("ERR_WRONG_ITEM_NAME"	, "-> You can only have alphanumeric symbols in your name,\n and first character should be a letter\n");

function performDataValidation() {

	global $winmain, $controls;

	$selection = wb_get_selected($controls['combo'][TK_COMBO_01NEW]);

	$errors = '';

	if($selection <= 1) {	
		//----Mod Aplicatie----
		$nume 	= wb_get_text($controls['edit'][TK_EDIT_01NAME]);
		$prefix = wb_get_text($controls['edit'][TK_EDIT_02PREFIX]);
				
		switch(true) {
			case (strlen( trim($nume) ) == 0): 
				$errors .= ERR_NO_APP_NAME; break;
				
			case (!preg_match("/^[a-zA-Z0-9]+$/", $nume)):
				$errors .= ERR_WRONG_APP_NAME; break;
		}

		switch(true) {
			case (strlen( trim($prefix) ) == 0):
				$errors .= ERR_NO_APP_PREFIX; break;
				
			case (strlen( trim($prefix) ) > 5):
				$errors .= ERR_LONG_APP_PREFIX; break;
				
			case ( !preg_match("/^[a-zA-Z0-9]+$/", $prefix) ):
				$errors .= ERR_WRONG_APP_PREFIX; break;
		}

	} else {
		//----Mod Element MVC/Helper----
		$nume = wb_get_text($controls['edit'][TK_EDIT_01NAME]);
		
		switch(true) {
			case (strlen( trim($nume) ) == 0):
				$errors .= ERR_NO_ITEM_NAME; break;
			
			case (!preg_match("/^[a-zA-Z0-9]+$/", $nume)):
				$errors .= ERR_WRONG_ITEM_NAME; break;
		}
	}
	
	// Verificam daca exista erori, daca exista 
	// le vom afisa si vom intoarce false
	if(!empty( $errors )) 
	{
		wb_message_box($winmain, $errors, "Errors Found!", WBC_STOP);
		return false;
	}

	return true;
}

function sendFormData() {

	global $winmain, $controls, $options_c02, $options_c03, $options_c04;

	$selection = wb_get_selected($controls['combo'][TK_COMBO_01NEW]);
	
	$data = array();
	$type = null;
	
	if($selection <= 1) {
		//----Mod Aplicatie----
		$data['name'] 	= wb_get_text($controls['edit'][TK_EDIT_01NAME]);
		$data['prefix'] = wb_get_text($controls['edit'][TK_EDIT_02PREFIX]);
		$data['lang'] 	= wb_get_text($controls['edit'][TK_EDIT_03LANG]);
		$data['copy'] 	= wb_get_text($controls['edit'][TK_EDIT_04COPY]);
		$data['desc'] 	= wb_get_text($controls['edit'][TK_EDIT_05DESC]);
		$data['keyw'] 	= wb_get_text($controls['edit'][TK_EDIT_06KEYW]);
		
		// Limba este necesara in modul complet pentru
		// generarea fisierelor de limba, daca nu se introduce
		// nimic vom genera o valoare generica
		if($data['lang']=="") $data['lang'] = 'en_GB';
		
		$tmp = wb_get_selected($controls['combo'][TK_COMBO_04DESIGN]);
		$data['tmpl'] = $options_c04[$tmp];
		
		if($selection != 1) {
			$tmp = wb_get_selected($controls['combo'][TK_COMBO_03TMPL]);
			$data['app_tmpl'] = $options_c03[$tmp];
			$type = TK_APP_TMPL;
		} elseif($selection == 1) {
			$type = TK_APP_EMPTY;
		}

	} else {
		//----Mod Element MVC/Helper----
		$data['name'] = wb_get_text($controls['edit'][TK_EDIT_01NAME]);

		$tmp = wb_get_selected($controls['combo'][TK_COMBO_02INSIDE]);
		$data['app'] = $options_c02[$tmp];

		switch($selection){
			case 2: $type = TK_ITEM_CONTROLLER_VIEW; break;
			case 3: $type = TK_ITEM_MODEL; break;
			case 4: $type = TK_ITEM_TABLE; break;
			case 5: $type = TK_ITEM_MODULE; break; 
		}
	}

	if(!is_null($type)) {
		Toolkit::tkCreate($data,$type);
	}
}

